//
//  UIImage+Extension.m
//  UIImageClipDemo
//
//  Created by 施文松 on 2018/4/20.
//  Copyright © 2018年 ShiWensong. All rights reserved.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

- (void)clipImageWithSize:(CGSize)size withFillColor:(UIColor *)filleColor withCompletete:(void(^)(UIImage *image))complete{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSTimeInterval start = CACurrentMediaTime();
        
        // 1. 创建上下文
        UIGraphicsBeginImageContextWithOptions(size, YES, 0);
        
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        
        // 2.设置填充颜色
        [filleColor setFill];
        UIRectFill(rect);
        
        // 3.利用贝塞尔做剪切效果
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
        [path addClip];
        
        // 4.绘制
        [self drawInRect:rect];
        
        // 5.获取结果
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        
        // 6. 关闭上下文
        UIGraphicsEndImageContext();
        
        NSLog(@"执行时间 == %f", CACurrentMediaTime() - start);
        
        // 7. 完成回调
        dispatch_async(dispatch_get_main_queue(), ^{
            if (complete) {
                complete(result);
            }
        });
    });
}

@end
